package Forms;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * _Main.java
 *
 * Created on Jan 11, 2013, 12:04:39 AM
 */


//import Classes.Grades;
//import Classes.MInterface;
//import Classes.Sections;
//import Classes.Student;
//import Classes.User;
import Classes.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Owner
 */
public class SectionA_Manage extends JFrame {
    static MInterface mi;
    String port;
    _Login l = new _Login();
    
    static User cUser;
    
    ArrayList<ClassLevel> clevelList;
    ArrayList<Sections> secList;
    ArrayList<Student> studList;
    ArrayList<Student> stud1;
    ArrayList<Student> stud2;
    SchoolYear sy;
    
    DefaultTableModel studtm1;
    DefaultTableModel studtm2;
    
    /** Creates new form _Main */
    public SectionA_Manage(User user, MInterface i) {
        
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        initComponents();
        mi = i;
        usernameL.setText(user.getName());
        cUser = user;
        port = l.serverAdd;        
        
        try {
            timeL.setText(mi.getCurrentTime());                
            dateL.setText(mi.getCurrentDate());
            
            String currsy = "";
            Calendar cal = Calendar.getInstance();
            int mo = cal.get(Calendar.MONTH) + 1;
            if(mo <= 3) {
                currsy = mi.getPast() + "-" + mi.getToday();
            } else {
                currsy = mi.getToday() + "-" + mi.getNext();
            }
            
            syl.setText(currsy);
            
            sy = (SchoolYear) mi.get("get-sy", currsy, "");
            loadClevel();
            loadSection();
            loadStuda();
            loadStudb();
        }catch(Exception e){
            System.out.println("Connection error!");
            e.printStackTrace();
        }
        
        ActionListener a = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    timeL.setText(mi.getCurrentTime());
                    dateL.setText(mi.getCurrentDate());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        };
        Timer t = new Timer(1000, a);
        t.start();
    }
    
    public void loadClevel() {
        try {
            clevelCB.removeAllItems();
            clevelList = (ArrayList<ClassLevel>) mi.getList2("getlist2-clevelcurrView", sy.getSyName(), "").clone();
            for (int x = 0; x < clevelList.size(); x++) {
                ClassLevel cc = clevelList.get(x);
                clevelCB.addItem(cc.getClassName() + " (" + cc.getCurriculum() + ")");
            }
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 
    
    public void loadSection() {
        try {
            secCB.removeAllItems();
            secList = (ArrayList<Sections>) mi.getList2("getlist2-secforclevel", clevelList.get(clevelCB.getSelectedIndex()).getCurrclID(), "").clone();
            for (int x = 0; x < secList.size(); x++) {
                Sections cc = secList.get(x);
                secCB.addItem(cc.getSect());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void loadStuda() {
        try {
            studList = (ArrayList<Student>) mi.getList3("getlist3-studforsecassgn", sy.getSyID() + "," + clevelList.get(clevelCB.getSelectedIndex()).getCurrID() + "," +clevelList.get(clevelCB.getSelectedIndex()).getClassID(), "").clone();
            studtm1 = new DefaultTableModel(new String[]{studTbl1.getColumnName(0), studTbl1.getColumnName(1), studTbl1.getColumnName(2)}, 0);
            studTbl1.setModel(studtm1);
            stud1 = (ArrayList<Student>) studList.clone();
            for (int x = 0; x < studList.size(); x++) {
                Student s = studList.get(x);
                
                studtm1.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
            }
            label.setText(studList.size() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void loadStudb() {
        try {
            stud2 = (ArrayList<Student>) mi.getList2("getlist2-studofsec", sy.getSyID() + "", secList.get(secCB.getSelectedIndex()).getSecID() + "").clone();
            studtm2 = new DefaultTableModel(new String[]{studTbl2.getColumnName(0), studTbl2.getColumnName(1), studTbl2.getColumnName(2)}, 0);
            studTbl2.setModel(studtm2);
            for (int x = 0; x < studList.size(); x++) {
                Student s = studList.get(x);
                
                studtm2.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
            }
            slot.setText((40-stud2.size()) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bannerL = new javax.swing.JLabel();
        dateL = new javax.swing.JLabel();
        timeL = new javax.swing.JLabel();
        logoutB = new javax.swing.JButton();
        usernameL = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane57 = new javax.swing.JScrollPane();
        studTbl1 = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex){
                return false;
            }
        };
        clevelCB = new javax.swing.JComboBox();
        yearLevelL10 = new javax.swing.JLabel();
        secassgnSearchB = new javax.swing.JButton();
        searchT = new javax.swing.JTextField();
        secCB = new javax.swing.JComboBox();
        yearLevelL15 = new javax.swing.JLabel();
        add = new javax.swing.JButton();
        rem = new javax.swing.JButton();
        jScrollPane58 = new javax.swing.JScrollPane();
        studTbl2 = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex){
                return false;
            }
        };
        yearLevelL63 = new javax.swing.JLabel();
        slot = new javax.swing.JLabel();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        yearLevelL65 = new javax.swing.JLabel();
        label = new javax.swing.JLabel();
        syl = new javax.swing.JLabel();
        syLbl1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Section Assignment Management");
        setResizable(false);

        bannerL.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/banner.jpg"))); // NOI18N
        bannerL.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        dateL.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        dateL.setText("mm/dd/yyyy");

        timeL.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        timeL.setText("hh:mm am/pm");

        logoutB.setText("Logout");
        logoutB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutBActionPerformed(evt);
            }
        });

        usernameL.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        usernameL.setText("username");

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        studTbl1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        studTbl1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Student ID", "Student Name", "Gen Ave"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        studTbl1.getTableHeader().setReorderingAllowed(false);
        studTbl1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                studTbl1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                studTbl1MouseEntered(evt);
            }
        });
        jScrollPane57.setViewportView(studTbl1);

        clevelCB.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clevelCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clevelCBActionPerformed(evt);
            }
        });

        yearLevelL10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        yearLevelL10.setText("Class Level:");

        secassgnSearchB.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        secassgnSearchB.setText("Search");
        secassgnSearchB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                secassgnSearchBMouseClicked(evt);
            }
        });

        searchT.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        searchT.setForeground(new java.awt.Color(153, 153, 153));
        searchT.setText("Search for a Section...");
        searchT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchTMouseClicked(evt);
            }
        });
        searchT.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                searchTKeyTyped(evt);
            }
        });

        secCB.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        secCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                secCBActionPerformed(evt);
            }
        });

        yearLevelL15.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        yearLevelL15.setText("Section:");

        add.setText("<html>Add<br>\n>></html>"); // NOI18N
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        rem.setText("<html>Remove<br>\n&nbsp;&nbsp;&nbsp;&lt;&lt;</html>"); // NOI18N
        rem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remActionPerformed(evt);
            }
        });

        studTbl2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        studTbl2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Student ID", "Student Name", "Gen Ave"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        studTbl2.getTableHeader().setReorderingAllowed(false);
        jScrollPane58.setViewportView(studTbl2);

        yearLevelL63.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        yearLevelL63.setText("Slots available:");

        slot.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        slot.setText("0");

        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelMouseClicked(evt);
            }
        });

        yearLevelL65.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        yearLevelL65.setText("No of Students:");

        label.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        label.setText("0");

        syl.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        syl.setText("0000-0000");

        syLbl1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        syLbl1.setText("School Year:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(yearLevelL65)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(label))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(searchT, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(secassgnSearchB)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(yearLevelL10)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(clevelCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(jScrollPane57, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 590, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(add)
                    .addComponent(rem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(yearLevelL63)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(slot)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(save)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(yearLevelL15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(secCB, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(syLbl1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(syl)
                        .addGap(12, 12, 12))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane58, javax.swing.GroupLayout.PREFERRED_SIZE, 611, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchT, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(yearLevelL10)
                            .addComponent(clevelCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(secassgnSearchB, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(secCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(yearLevelL15)
                            .addComponent(syl)
                            .addComponent(syLbl1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane58, 0, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane57, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(save, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(yearLevelL63)
                                .addComponent(slot)
                                .addComponent(yearLevelL65)
                                .addComponent(label)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rem, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(210, 210, 210))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bannerL, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(dateL)
                        .addGap(18, 18, 18)
                        .addComponent(timeL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(usernameL)
                        .addGap(18, 18, 18)
                        .addComponent(logoutB)
                        .addGap(28, 28, 28))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 1304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bannerL, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeL)
                    .addComponent(dateL)
                    .addComponent(logoutB, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(usernameL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelMouseClicked
        this.setVisible(false);
//        new _UAdmin(3, cUser, mi).setVisible(true);
}//GEN-LAST:event_cancelMouseClicked

    private void logoutBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutBActionPerformed
        // TODO add your handling code here:
        int x = JOptionPane.showConfirmDialog(null,"Are you sure you want to logout already?","Alert!",JOptionPane.YES_NO_OPTION);        
        if(x == JOptionPane.YES_OPTION){
            try {
                mi.logout(cUser.getUsername());
                mi.clientActivity(cUser.getUsername(), "logout");
                this.setVisible(false);
                new _Login().setVisible(true);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_logoutBActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        if (studTbl1.getRowCount() == 0) {
            JOptionPane.showMessageDialog(null, "There is nothing to add!", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int x = studTbl1.getSelectedRowCount();
            if (x >= 1) {
                int[] rows = studTbl1.getSelectedRows();
                add2(rows);
                rem1(rows);
            } else {
                JOptionPane.showMessageDialog(null, "No row selected!", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_addActionPerformed

    private void remActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remActionPerformed
        if (studTbl2.getRowCount() == 0) {
            JOptionPane.showMessageDialog(null, "There is nothing to remove!", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int x = studTbl2.getSelectedRowCount();
            if (x >= 1) {
                int[] rows = studTbl2.getSelectedRows();
                add1(rows);
                rem2(rows);
            } else {
                JOptionPane.showMessageDialog(null, "No row selected!", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_remActionPerformed

    private void clevelCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clevelCBActionPerformed
        loadSection();
        loadStuda();
        loadStudb();
    }//GEN-LAST:event_clevelCBActionPerformed

    private void searchTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchTMouseClicked
//        searchT.setText("");
//        searchT.setForeground(Color.black);
    }//GEN-LAST:event_searchTMouseClicked

    private void secassgnSearchBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_secassgnSearchBMouseClicked
//        search();
    }//GEN-LAST:event_secassgnSearchBMouseClicked

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        boolean s = false;
        for(int x = 0; x < stud2.size(); x++) {
            try {
                Student ss = stud2.get(x);
                ss.setSecID(secList.get(secCB.getSelectedIndex()).getSecID() + "");
                s = mi.edit("edit-studsec", ss, cUser);
            } catch (Exception ex) {
                Logger.getLogger(SectionA_Manage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if(s) {
            JOptionPane.showMessageDialog(null, "Student/s successfully assigned to " + clevelCB.getSelectedItem().toString() + " : " + secCB.getSelectedItem().toString(), "Success", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_saveActionPerformed

    private void searchTKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchTKeyTyped
//        if(evt.getKeyChar() == KeyEvent.VK_ENTER) search();
//        String s = "[a-zA-Z0-9\\._\\-]{3,}";
//        char c = evt.getKeyChar();
//    
//        if(c == KeyEvent.VK_SPACE) {
//            if(searchT.getText().isEmpty()) {
//                evt.consume();
//                JOptionPane.showMessageDialog (null, "Space bar not allowed as first character.", "Error", JOptionPane.ERROR_MESSAGE);
//            }
//        }
    }//GEN-LAST:event_searchTKeyTyped

    private void studTbl1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_studTbl1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_studTbl1MouseClicked

    private void secCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_secCBActionPerformed
        
    }//GEN-LAST:event_secCBActionPerformed

    private void studTbl1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_studTbl1MouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_studTbl1MouseEntered

//    public void search() {
//        try {
//            studList = mi.getList("sectionsG");
//            ArrayList<Sections> s = new ArrayList<Sections>();
//            String str = searchT.getText().toLowerCase();
//            for(int x = 0; x < studList.size(); x++) {
//                Sections ss = (Sections) studList.get(x);
//                if(ss.getStud().toLowerCase().contains(str) || ss.getStudID().toLowerCase().contains(str)) {
//                    s.add(ss);
//                }
//            }
//
//            secassgnNumStudL.setText(s.size() + "");
//            dtmAuto = new DefaultTableModel(new String[] {secassgnStudTbl1.getColumnName(0), secassgnStudTbl1.getColumnName(1)}, 0);
//            secassgnStudTbl1.setModel(dtmAuto);
//            for(int x = 0; x < s.size(); x++) {
//                dtmAuto.insertRow(x, new Object[] {s.get(x).getStudIDNo(), s.get(x).getStud()});
//            }          
//        } catch(Exception e) {
//            e.printStackTrace();
//        }
//    }
    
    public void add1(int[] arr) {
        for (int x = 0; x < arr.length; x++) {
            Student s = stud2.get(arr[x]);
            stud1.add(s);
            studtm1.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
        }
        label.setText(stud1.size() + "");
    }

    public void add2(int[] arr) {
        for (int x = 0; x < arr.length; x++) {
            Student s = stud1.get(arr[x]);
            stud2.add(s);
            studtm2.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
        }
        slot.setText((40-stud2.size()) + "");
    }

    public void rem1(int[] ii) {
        ArrayList<Student> stu = (ArrayList<Student>) stud1.clone();
        int j = ii.length - 1;
        for (int u = 0; u < j; u++, j--) {
            int t = ii[u];
            ii[u] = ii[j];
            ii[j] = t;
        }
        for (int x = 0; x < ii.length; x++) {
            stu.remove(ii[x]);
        }

        stud1 = (ArrayList<Student>) stu.clone();
        try {
            studtm1 = new DefaultTableModel(new String[]{studTbl1.getColumnName(0), studTbl1.getColumnName(1), studTbl1.getColumnName(2)}, 0);
            studTbl1.setModel(studtm1);
            for (int x = 0; x < stud1.size(); x++) {
                Student s = stud1.get(x);
                studtm1.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
            }
            label.setText(stud1.size() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void rem2(int[] ii) {
        ArrayList<Student> stu = (ArrayList<Student>) stud2.clone();
        int j = ii.length - 1;
        for (int u = 0; u < j; u++, j--) {
            int t = ii[u];
            ii[u] = ii[j];
            ii[j] = t;
        }
        for (int x = 0; x < ii.length; x++) {
            stu.remove(ii[x]);
        }

        stud2 = (ArrayList<Student>) stu.clone();
        try {
            studtm2 = new DefaultTableModel(new String[]{studTbl2.getColumnName(0), studTbl2.getColumnName(1), studTbl2.getColumnName(2)}, 0);
            studTbl2.setModel(studtm2);
            for (int x = 0; x < stud2.size(); x++) {
                Student s = stud2.get(x);
                studtm2.addRow(new Object[]{s.getSID(), s.getName(), s.getGeneralAverage()});
            }
            slot.setText((40-stud2.size()) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SectionA_Manage(cUser, mi).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JLabel bannerL;
    private javax.swing.JButton cancel;
    private javax.swing.JComboBox clevelCB;
    private javax.swing.JLabel dateL;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane57;
    private javax.swing.JScrollPane jScrollPane58;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel label;
    private javax.swing.JButton logoutB;
    private javax.swing.JButton rem;
    private javax.swing.JButton save;
    private javax.swing.JTextField searchT;
    private javax.swing.JComboBox secCB;
    private javax.swing.JButton secassgnSearchB;
    private javax.swing.JLabel slot;
    private javax.swing.JTable studTbl1;
    private javax.swing.JTable studTbl2;
    private javax.swing.JLabel syLbl1;
    private javax.swing.JLabel syl;
    private javax.swing.JLabel timeL;
    private javax.swing.JLabel usernameL;
    private javax.swing.JLabel yearLevelL10;
    private javax.swing.JLabel yearLevelL15;
    private javax.swing.JLabel yearLevelL63;
    private javax.swing.JLabel yearLevelL65;
    // End of variables declaration//GEN-END:variables
}
